package com.pic.mgt.haishin.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.haishin.action.TerminalMgtAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class TerminalMgtCommandPerformer extends AbstractCommandPerformer {
	
	private final String commandName = "trmmgt";
	private final String commandDescritpion = "terminals management.";
	
	private TerminalMgtAction terminalMgtAction;

	@Override
	protected void addOption(Options options) {
		options.addOption("add", false, "add a terminal. (req: td ; opt: es, ee)");
		options.addOption("rmv", false, "rmv a terminal. (req: td)");
		options.addOption("chk", false, "check terminal existed in system management list and show basic info. (req: td)");
		options.addOption("td", true, "assign terminal id.");
		options.addOption("es", true, "assign effective start time. (format: yyyyMMddHHmmss) (notice: if ignored, the default is current time)");
		options.addOption("ee", true, "assign effective end time. (format: yyyyMMddHHmmss) (notice: (1) if ignored, means permanent (2) if used, must be with 'es')");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("add")) {
			if (!commandLine.hasOption("td")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide terminal id.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			boolean isAddTerminalSuccessful;
			
			if (commandLine.hasOption("es")) {
				if (commandLine.hasOption("ee")) {
					isAddTerminalSuccessful =
						this.terminalMgtAction.addTerminal(
									commandLine.getOptionValue("td"),
									commandLine.getOptionValue("es"),
									commandLine.getOptionValue("ee"));
				}
				else {
					isAddTerminalSuccessful =
						this.terminalMgtAction.addTerminal(
									commandLine.getOptionValue("td"),
									commandLine.getOptionValue("es"));
				}
			}
			else { // 沒有 es 參數
				if (commandLine.hasOption("ee")) {
					return
					super.getReplyMessageWithReplyCode(
							"Assigning effective end time ('ee') must also provide effective start time ('es') .",
							this.COMMAND_PERFORM_FAILED_CODE);
				}
				else {
					isAddTerminalSuccessful =
						this.terminalMgtAction.addTerminal(commandLine.getOptionValue("td"));
				}
			}
			
			if (isAddTerminalSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("rmv")) {
			if (!commandLine.hasOption("td")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide terminal id.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			boolean isRemoveTerminalSuccessful =
				this.terminalMgtAction.removeTerminal(commandLine.getOptionValue("td"));
			
			if (isRemoveTerminalSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("chk")) {
			if (!commandLine.hasOption("td")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide terminal id.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			return 
				this.terminalMgtAction.checkTerminalId(commandLine.getOptionValue("td"));
		}
		else {
			return
			super.getReplyMessageWithReplyCode(
					"did not specify management action (option: add, rmv, or chk).",
					this.COMMAND_PERFORM_FAILED_CODE);
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}
	
	// --- getter and setter ---

	public void setTerminalMgtAction(TerminalMgtAction terminalMgtAction) {
		this.terminalMgtAction = terminalMgtAction;
	}

}
